import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class demo01 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 有返回值的 supplyAsync 异步回调
        // 类似ajax，有成功和失败的回调
        // 失败会返回错误信息；
        CompletableFuture<Integer> completableFuture =
                CompletableFuture.supplyAsync(() -> {
                    System.out.println(Thread.currentThread().getName() + "supplyAsync=>Integer");
//                    int i = 10 / 0;
                    return 1024;
                });
//        System.out.println(completableFuture.whenComplete((t, u) -> {
//            System.out.println("t=>" + t); // 正常的返回结果
//            System.out.println("u=>" + u); // 错误信息：java.util.concurrent.CompletionException: java.lang.ArithmeticException: / by zero
//        }).exceptionally((e) -> {//如果失败执行以下操作
//            System.out.println(e.getMessage());
//            return 233; // 失败的返回值
//        }).get());
    }
}
